<?php

	error_reporting(0);
	session_start();
	error_reporting(1);
	
	$id = $_GET['id'];

	require_once "../common/PinSQL.obj";
	$pinSQL = new PinSQL();
	
	$username = $_SESSION['username'];
	
	$pinSQL->Query("SELECT * FROM messages WHERE (touser='$username' OR fromuser='$username') 
								AND id='$id'");
	
 	if ( $pinSQL->GetNumRows() )
 	{
	 	
	 	$row = $pinSQL->FetchNextRow();
		$prettydate = date(  'F j, Y', strtotime( $row['date'] )  );
  		$prettytime = date(  'g:ia', strtotime( $row['date'] )  );
  		
		if ($row['touser'] == $username)
		{
			$mode = 'inbox';
			$pinSQL->Query("UPDATE messages SET isread='1' WHERE id='$id'");
		} else {
			$mode = 'outbox';
		}
  		
?>

<head>
	<link rel="stylesheet" type="text/css" href="profile_inner.css" />
</head>

<img src="/img/icons/back.gif"><a href="frame.php?page=profile"> Return to profile </a>

<div class="padded_text">

<div class="head">
	<?php echo ($row['subject']) ? $row['subject'] : "[no subject]"; ?>
</div>

<?php
		if ($mode == 'inbox')
		{ ?>
<p />Sent by: <span class="redtext"> <?php echo $row['fromuser']; ?> </span>
<?php	} else { ?>
<p />Sent to: <span class="redtext"> <?php echo $row['touser']; ?> </span>
<?php	} ?>

<br />at <?php echo $prettytime; ?> on <?php echo $prettydate; ?>

<blockquote>
	<?php echo nl2br($row['message']); ?>
</blockquote>


<?php
		if ($mode == 'inbox')
		{ ?>
<p /><img src="/img/arrow_yellow.gif"><a href="frame.php?page=composemessage&touser=<?php echo $row['fromuser']; ?>&subject=Re: <?php echo $row['subject']; ?>">Reply to this message</a>
<?php	} else { ?>
<p /><img src="/img/arrow_blue.gif"><a href="frame.php?page=composemessage&touser=<?php echo $row['touser']; ?>&subject=F-up: <?php echo $row['subject']; ?>">Send another message to <?php echo $row['touser']; ?></a>
<?php	} ?>
<br /><img src="/img/icons/back.gif">


<?php 
	
		if ($mode == 'inbox')
		{
		?>
			<a href="frame.php?page=inbox">Back to my inbox</a>
		<?php
		} else {
		?>
			<a href="frame.php?page=outbox">Back to my outbox</a>
		<?php
		}
		
	} else {
		
		echo "Invalid message chosen.  This message either does not exist or you are unauthorized to view the contents of this message.  Basically, it is NONE OF YOUR BUSINESS!!!";
 	}

?>

</div>
